﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SimpleGameData.GameObjects
{
    public class GameObjectData
    {
        #region Fields

        // The texture path of this game object's texture
        protected string texturePath;
        // The texture used for this game object
        protected Texture2D texture;
        // The color used for the texture
        protected Color tintColor = Color.White;
        // The rotation for this game object
        protected float rotation = 0.0f;
        // The scale for this game objects sprite
        protected Vector2 scale = new Vector2(1.0f, 1.0f);
        // The position for this game object
        protected Vector2 position;
        // The motion vector for this game object
        protected Vector2 direction;
        // The velocity for this game object
        protected float velocity;

        #endregion

        #region Properties

        /// <summary>
        /// The path of the texture represented by a string.
        /// </summary>
        public string TexturePath
        {
            get { return texturePath; }
            set { texturePath = value; }
        }

        /// <summary>
        /// The texture of the DrawableGameObject.
        /// </summary>
        [ContentSerializerIgnore]
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }

        /// <summary>
        /// The rotation of the DrawableGameObject.
        /// </summary>
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        /// <summary>
        /// The X, Y scale of the DrawableGameObject. (1.0 is 100% = init value)
        /// </summary>
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        /// <summary>
        /// The color tint of the texture of the DrawableGameObject.
        /// </summary>
        public Color TintColor
        {
            get { return tintColor; }
            set { tintColor = value; }
        }

        /// <summary>
        /// The X, Y position of the game object.
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        /// <summary>
        /// The direction of the MoveableGameObject. Needs normalizing when used to prevent diagonal movement glitch.
        /// </summary>
        [ContentSerializerIgnore]
        public Vector2 Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        /// <summary>
        /// The speed at which the MoveableGameObject moves.
        /// </summary>
        public float Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        /// <summary>
        /// The Center of the game object's texture. i.e. used as rotation origin.
        /// </summary>
        [ContentSerializerIgnore]
        public Vector2 SpriteCenter
        {
            get
            {
                return new Vector2(
                  (float)texture.Width / 2f,
                  (float)texture.Height / 2f);
            }
        }

        /// <summary>
        /// The Center of game object's texture plus the position of the game object, altered by scale.
        /// </summary>
        [ContentSerializerIgnore]
        public Vector2 SpriteCenterPosition
        {
            get { return position + (SpriteCenter * scale); }
        }

        #endregion

        #region Constructor

        public GameObjectData() { }

        #endregion
    }

    #region ContentTypeReader Class

    public class GameObjectDataReader : ContentTypeReader<GameObjectData>
    {
        protected override GameObjectData Read(ContentReader input,
            GameObjectData existingInstance)
        {
            if (existingInstance == null)
            {
                throw new ArgumentNullException("existingInstance");
            }

            existingInstance.TexturePath = input.ReadString();
            if (existingInstance.TexturePath != string.Empty)
            {
                existingInstance.Texture =
                    input.ContentManager.Load<Texture2D>(existingInstance.TexturePath);
            }
            existingInstance.TintColor = input.ReadColor();
            existingInstance.Rotation = input.ReadSingle();
            existingInstance.Scale = input.ReadVector2();
            existingInstance.Position = input.ReadVector2();
            existingInstance.Velocity = input.ReadSingle();

            return existingInstance;
        }
    }

    #endregion
}
